package com.muxue.module.ai.service.chat.impl;

import cn.hutool.core.util.ObjUtil;
import com.muxue.common.core.model.CommonResult;
import com.muxue.common.core.page.PageResult;
import com.muxue.module.ai.controller.chat.vo.message.AiChatMessagePageReqVO;
import com.muxue.module.ai.controller.chat.vo.message.AiChatMessageSendReqVO;
import com.muxue.module.ai.controller.chat.vo.message.AiChatMessageSendRespVO;
import com.muxue.module.ai.mapper.dataobject.chat.AiChatMessageDO;
import com.muxue.module.ai.mapper.mysql.chat.AiChatMessageMapper;
import com.muxue.module.ai.service.chat.AiChatConversationService;
import com.muxue.module.ai.service.chat.AiChatMessageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.Collection;
import java.util.Map;

import static com.muxue.common.exception.ServiceException.exception;
import static com.muxue.module.ai.model.enums.ErrorCodeConstants.CHAT_CONVERSATION_NOT_EXISTS;
import static com.muxue.module.ai.model.enums.ErrorCodeConstants.CHAT_MESSAGE_NOT_EXIST;


/**
 * AI 聊天消息 Service 实现类
 *
 * @author fansili
 */
@Service
@Slf4j
public class AiChatMessageServiceImpl implements AiChatMessageService {

    @Resource
    private AiChatMessageMapper chatMessageMapper;

    @Resource
    private AiChatConversationService chatConversationService;

//    @Transactional(rollbackFor = Exception.class)
//    public AiChatMessageSendRespVO sendMessage(AiChatMessageSendReqVO sendReqVO, Long userId) {
//        return null;
//    }


    @Override
    public AiChatMessageSendRespVO sendMessage(AiChatMessageSendReqVO sendReqVO, Long userId) {
        return null;
    }

    public Flux<CommonResult<AiChatMessageSendRespVO>> sendChatMessageStream(AiChatMessageSendReqVO sendReqVO, Long userId) {
        throw exception(CHAT_CONVERSATION_NOT_EXISTS);
    }






//

    @Override
    public void deleteChatMessage(Long id, Long userId) {
        // 1. 校验消息存在
        AiChatMessageDO message = chatMessageMapper.selectById(id);
        if (message == null || ObjUtil.notEqual(message.getUserId(), userId)) {
            throw exception(CHAT_MESSAGE_NOT_EXIST);
        }
        // 2. 执行删除
        chatMessageMapper.deleteById(id);
    }

    @Override
    public void deleteChatMessageByConversationId(Long conversationId, Long userId) {
        throw exception(CHAT_MESSAGE_NOT_EXIST);
    }

    @Override
    public void deleteChatMessageByAdmin(Long id) {
        // 1. 校验消息存在
        AiChatMessageDO message = chatMessageMapper.selectById(id);
        if (message == null) {
            throw exception(CHAT_MESSAGE_NOT_EXIST);
        }
        // 2. 执行删除
        chatMessageMapper.deleteById(id);
    }

    @Override
    public Map<Long, Integer> getChatMessageCountMap(Collection<Long> conversationIds) {
        throw exception(CHAT_MESSAGE_NOT_EXIST);
    }


    public PageResult<AiChatMessageDO> getChatMessagePage(AiChatMessagePageReqVO pageReqVO) {
        throw exception(CHAT_MESSAGE_NOT_EXIST);
    }

}
